// 直接跟新多个state 方法
// import Vue from 'vue'
import {
  RECEIVE_TOKEN,
  SET_TOKEN,
  RECEIVE_USER_INFO,
  CHANGE_SETTING,
  TOGGLE_SIDEBAR,
  SET_SIZE,
  DEL_ALL_CACHED_VIEWS,
  ADD_VISITED_VIEW,
  ADD_CACHED_VIEW,
  UPDATE_VISITED_VIEW,
  DEL_CACHED_VIEW,
  DEL_VISITED_VIEW,
  DEL_OTHERS_VISITED_VIEWS,
  DEL_OTHERS_CACHED_VIEWS,
  DEL_ALL_VISITED_VIEWS,
  SET_ROLES,
  SET_PERMISSIONS,
  SET_AVATAR,
  SET_NAME,
  SET_ROUTERS
} from './mutation-types'
import {constantRouters} from '../router'
export default {
  // 获取token
  [RECEIVE_TOKEN] (state, {token}) {
    state.user.token = token
  },
  // 设置token
  [SET_TOKEN] (state, {token}) {
    state.user.token = token
  },
  [RECEIVE_USER_INFO] (state, {userInfo}) {
    // 存储用户信息到 localstorage
    // TODO
  },
  [CHANGE_SETTING] (state, {key, value}) {
    if (state.settings.hasOwnProperty(key)) {
      state.settings.showSettings = value
    }
  },
  [TOGGLE_SIDEBAR] (state) {
    state.app.sidebar.opened = !state.app.sidebar.opened
    state.app.sidebar.withoutAnimation = false
    if (state.app.sidebar.opened) {
      // TODO
      // Cookies.set('sidebarStatus', 1)
    } else {
      // TODO
      // Cookies.set('sidebarStatus', 1)
    }
  },
  [SET_SIZE] (state, size) {
    state.app.size = size
    // set cookie TODO
  },
  [DEL_ALL_CACHED_VIEWS] (state) {
    state.tagsView.cachedViews = []
  },
  [ADD_VISITED_VIEW]: (state, view) => {
    if (state.tagsView.visitedViews.some(v => v.path === view.path)) return
    state.tagsView.visitedViews.push(
      Object.assign({}, view, {
        title: view.meta.title || 'no-name'
      }))
  },
  [ADD_CACHED_VIEW] (state, view) {
    if (state.tagsView.cachedViews.includes(view.name)) return
    if (!view.meta.noCache) {
      state.tagsView.cachedViews.push(view.name)
    }
  },
  [UPDATE_VISITED_VIEW] (state, view) {
    for (let v of state.tagsView.visitedViews) {
      if (v.path === view.path) {
        v = Object.assign(v, view)
        break
      }
    }
  },
  [DEL_CACHED_VIEW] (state, view) {
    const index = state.cachedViews.indexOf(view.name)
    index > -1 && state.cachedViews.splice(index, 1)
  },
  [DEL_VISITED_VIEW] (state, view) {
    for (const [i, v] of state.visitedViews.entries()) {
      if (v.path === view.path) {
        state.visitedViews.splice(i, 1)
        break
      }
    }
  },
  [DEL_OTHERS_VISITED_VIEWS] (state, view) {
    state.visitedViews = state.visitedViews.filter(v => {
      return v.meta.affix || v.path === view.path
    })
  },
  [DEL_OTHERS_CACHED_VIEWS] (state, view) {
    const index = state.cachedViews.indexOf(view.name)
    if (index > -1) {
      state.cachedViews = state.cachedViews.slice(index, index + 1)
    } else {
      state.cachedViews = []
    }
  },
  [DEL_ALL_VISITED_VIEWS] (state) {
    // keep affix tags
    const affixTags = state.visitedViews.filter(tag => tag.meta.affix)
    state.visitedViews = affixTags
  },
  [SET_ROLES] (state, roles) {
    state.user.roles = roles
  },
  [SET_PERMISSIONS] (state, permissions) {
    state.user.permissions = permissions
  },
  [SET_NAME] (state, name) {
    state.user.name = name
  },
  [SET_AVATAR] (state, avatar) {
    state.user.avatar = avatar
  },
  [SET_ROUTERS] (state, result) {
    state.permission.addRoutes = result
    state.permission.routes = constantRouters.concat(result)
  }
}
